#include <iostream>
#include <queue>
#include <set>
using namespace std;

const int N1 = 5010;
const int N2 = 5000007;
int n, c, cnt;
int rooms[N2];

struct Group
{
    string sch;
    int id;
    int cnt;
    bool operator<(const Group &g) const
    {
        return this->cnt < g.cnt;
    }
} groups[N1];

priority_queue<Group> heap;
set<int> st[N1];

int main()
{
    cin >> n >> c;
    for (int i = 0; i < n; i++)
    {
        string sch;
        int cnt;
        cin >> sch >> cnt;
        groups[i].id = i;
        groups[i].sch = sch;
        groups[i].cnt = cnt;
        heap.push(groups[i]);
    }
    while (!heap.empty())
    {
        Group top = heap.top();
        heap.pop();
        if (top.cnt >= c)
        {
            rooms[cnt] = c;
            top.cnt -= c;
            st[top.id].insert(cnt);
            cnt++;
        }
        else
        {
            int i = 0;
            for (; i < cnt; i++)
            {
                if ((c - rooms[i]) >= top.cnt)
                {
                    rooms[i] += top.cnt;
                    top.cnt = 0;
                    st[top.id].insert(i);
                    break;
                }
            }
            if (i == cnt)
            {
                rooms[cnt] = top.cnt;
                st[top.id].insert(cnt);
                top.cnt = 0;
                cnt++;
            }
        }
        if (top.cnt)
        {
            heap.push(top);
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << groups[i].sch << " " << st[i].size() << endl;
    }
    cout << cnt;
    return 0;
}